(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.custAcctType.updateController', updateController);
	updateController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'custAcctTypeService' ];
	function updateController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, custAcctTypeService) {
		var ctrl = this, custAcctTypeInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.custAcctType = {};

		ctrl.init = function() {
			var params = {
					custAcctTypeCode : custAcctTypeInfo.custAcctTypeCode
			};
			authService.update(params).then(function(response) {
				ctrl.custAcctType = response.data || custAcctTypeInfo;
			});
		};

		ctrl.updateSave = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.custAcctType.taskId) {
				return authService.updateContinueSave(ctrl.custAcctType).then(saveSuccessful, saveFailed);
			}  else {
				return authService.updateSave(ctrl.custAcctType).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.updateSubmit = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.custAcctType.taskId) {
				return authService.updateContinueSubmit(ctrl.custAcctType).then(submitSuccessful, submitFailed);
			}  else {
				return authService.updateSubmit(ctrl.custAcctType).then(submitSuccessful, submitFailed);
			}
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.custAcctType.list',
					'state' : 'cda.custAcctType.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				angular.extend(ctrl.custAcctType, response.data.model);
			}else if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}

		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);